#include<iostream>
#include<sw/redis++/redis++.h>
#include<vector>
#include<string>
#include<thread>
#include<chrono>
#include<set>
#include<unordered_map>
#include"Util.hpp"
using namespace std;
using namespace chrono_literals;


void test1(sw::redis::Redis& redis)
{
    cout << "hset" << endl;
    redis.flushall();
   //一个键值对
   redis.hset("key","f1","111");
   //pair一个键值对
   redis.hset("key",{"f2","222"});
   //多个键值对 初始化列表，每个元素都是pair
   redis.hset("key",{
       std::make_pair("f3","333"),
       std::make_pair("f4","444")
   });
   //多个键值对 迭代器区间
   vector<pair<string,string>> vp = {{"f5","555"},{"f6","666"}};
   redis.hset("key",vp.begin(),vp.end());

   auto ret1 = redis.hget("key","f4");
   if(ret1) cout << ret1.value() << endl;

   auto ret2 = redis.hget("key","f11");
   if(!ret2) cout <<"不存在" << endl;
   
}

void test2(sw::redis::Redis& redis)
{
    cout << "hexists" << endl;
    redis.flushall();

     redis.hset("key","f1","111");
     redis.hset("key","f2","222");
     redis.hset("key","f3","333");

     bool ret = redis.hexists("key","f2");
     if(ret) cout << "存在" << endl;
     else cout << "不存在" << endl;  
}

void test3(sw::redis::Redis& redis)
{
    cout << "hdel" << endl;
    redis.flushall();

     redis.hset("key","f1","111");
     redis.hset("key","f2","222");
     redis.hset("key","f3","333");

     cout << "before del len: " << redis.hlen("key") << endl;;
     
     auto ret = redis.hdel("key","f4");
     cout << "first del num: " << ret << endl;
     //初始化列表
     ret = redis.hdel("key",{"f1","f2"});
     cout << "second del num: " << ret << endl;

     cout << "after del len: " << redis.hlen("key") << endl;;
 }

void test4(sw::redis::Redis& redis)
{
    cout << "hkeys和hvals" << endl;
     redis.flushall();

     redis.hset("key","f1","111");
     redis.hset("key","f2","222");
     redis.hset("key","f3","333");

     vector<string> fields;
     redis.hkeys("key",std::back_inserter(fields));
     printContainer(fields);

     vector<string> values;
     redis.hvals("key",std::back_inserter(values));
     printContainer(values);
}

void test5(sw::redis::Redis& redis)
{
    cout << "hmget和hmset" << endl;
    redis.flushall();

    //1.hmset
    //初始化列表
    redis.hmset("key",{std::make_pair("f1","111"),std::make_pair("f2","222")});
    //迭代器区间
    vector<pair<string,string>> vp = {
        std::make_pair("f3","333"),
        std::make_pair("f4","444")
    };
    redis.hmset("key",vp.begin(),vp.end());

    //2.hmget
    vector<string> result;
    redis.hmget("key",{"f1","f3","f4"},std::back_inserter(result)); //也能用迭代器
    printContainer(result);
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    test5(redis);
    return 0;
}